package cn.MaySoon.zhuansheng;

import org.bukkit.Bukkit;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class handle {

        public static String 小修为丹(){
            return  "§6§5§4§b§l修为丹(小)";
        }

        public static  void downloadPlayerData(Player p){
            File file = new File(main.plugin.getDataFolder() + "/PlayerData/" + p.getName() + ".yml");
            YamlConfiguration data = YamlConfiguration.loadConfiguration(file);
            int level = data.getInt("level");
            int xiuwei = data.getInt("xiuwei");
            zhuansheng zhuansheng = new zhuansheng(level,xiuwei,p.getName());
            main.plugin.PlayerZhaunSheng.put(p,zhuansheng);
        }


        public static void updatePlayerXiuwei(Player p,int addxiuwei){
            zhuansheng zhuansheng = main.PlayerZhaunSheng.get(p);
            int xiuwei = zhuansheng.getXiuwei();
            xiuwei = xiuwei+addxiuwei;
            zhuansheng.setXiuwei(xiuwei);

        }

    public static void duihuanPlayerXiuwei(Player p,int addxiuwei){
        zhuansheng zhuansheng = main.PlayerZhaunSheng.get(p);
        int xiuwei = zhuansheng.getXiuwei();
        xiuwei = xiuwei+addxiuwei;
        zhuansheng.setduihuanXiuwei(xiuwei);

    }
        public static void savePlayerData(String playername,int level,int xiuwei){
            File file = new File(main.plugin.getDataFolder() + "/PlayerData/" + playername + ".yml");
            YamlConfiguration data = YamlConfiguration.loadConfiguration(file);
            data.set("level",level);
            data.set("xiuwei",xiuwei);
            try {
                data.save(file);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }



        public static void updatePlayerLevel(Player p){
            zhuansheng zhuansheng =main.PlayerZhaunSheng.get(p);
            int xiuwei =zhuansheng.getXiuwei();
            int level = zhuansheng.getLevel();
            int needxiuwei = main.config.getInt("zhuansheng."+(level+1)+".xiuwei");
            if (xiuwei>=needxiuwei){
                int leftxiuwei = 0;
                leftxiuwei = xiuwei-needxiuwei;
                level = level+1;
                zhuansheng.setLevel(level);
                zhuansheng.setXiuwei(leftxiuwei);
                AP.updatePlayerAttribute(p);
                p.sendMessage("§a突破成功,转生等级达到"+level+"转");
            }
            else{
                p.sendMessage("§e您的修为不足以突破");
                return;
            }
        }

        public static  int getUplevelNeedXiuwei(Player p){
            zhuansheng zhuansheng =main.PlayerZhaunSheng.get(p);
            int xiuwei =zhuansheng.getXiuwei();
            int level = zhuansheng.getLevel();
            int needxiuwei = main.config.getInt("zhuansheng."+(level+1)+".xiuwei");
            return needxiuwei;
        }

        public static int canbuyxiuweidan(Player p){
            File file = new File(main.plugin.getDataFolder() + "/PlayerData/" + p.getName() + ".yml");
            YamlConfiguration data = YamlConfiguration.loadConfiguration(file);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date day = new Date();
            String nowday = sdf.format(day);
            int buyed = data.getInt("buy."+nowday+".xiuwei");
            return 5-buyed;
        }
    public static int canbuydaxiuweidan(Player p){
        File file = new File(main.plugin.getDataFolder() + "/PlayerData/" + p.getName() + ".yml");
        YamlConfiguration data = YamlConfiguration.loadConfiguration(file);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date day = new Date();
        String nowday = sdf.format(day);
        int buyed = data.getInt("buy."+nowday+".daxiuwei");
        return 5-buyed;
    }
    public static int canexchangexiuwei(Player p){
        File file = new File(main.plugin.getDataFolder() + "/PlayerData/" + p.getName() + ".yml");
        YamlConfiguration data = YamlConfiguration.loadConfiguration(file);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date day = new Date();
        String nowday = sdf.format(day);
        int buyed = data.getInt("buy."+nowday+".money");
        return 3-buyed;
    }


        public static boolean buyxiuweidan(Player p){
            File file = new File(main.plugin.getDataFolder() + "/PlayerData/" + p.getName() + ".yml");
            YamlConfiguration data = YamlConfiguration.loadConfiguration(file);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date day = new Date();
            String nowday = sdf.format(day);

             int buyed = data.getInt("buy."+nowday+".xiuwei");
             if (buyed>=5){
                 p.sendMessage("§a§l今日此丹药购买已达到最大上限");
                 return  false;
             }
             if (main.playerPoints.getAPI().look(p.getUniqueId())>10) {
             main.playerPoints.getAPI().take(p.getUniqueId(),10);
                 buyed = buyed + 1;
                 data.set("buy."+nowday+".xiuwei",buyed);
                 duihuanPlayerXiuwei(p,1000);
                zhuansheng z =main.PlayerZhaunSheng.get(p);
                int level = z.getLevel();
                int xiuwei = z.getXiuwei();
                 data.set("level",level);
                 data.set("xiuwei",xiuwei);
                 try {
                     data.save(file);
                    p.sendMessage("§a购买修为丹成功");
                    return true;
                 } catch (IOException e) {
                     e.printStackTrace();
                 }
             }

            return true;
        }
    public static boolean buydaxiuweidan(Player p){
        File file = new File(main.plugin.getDataFolder() + "/PlayerData/" + p.getName() + ".yml");
        YamlConfiguration data = YamlConfiguration.loadConfiguration(file);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date day = new Date();
        String nowday = sdf.format(day);

        int buyed = data.getInt("buy."+nowday+".daxiuwei");
        if (buyed>=5){
            p.sendMessage("§a§l今日此丹药购买已达到最大上限");
            return  false;
        }
        if (main.playerPoints.getAPI().look(p.getUniqueId())>100) {
            main.playerPoints.getAPI().take(p.getUniqueId(),100);
            buyed = buyed + 1;
            data.set("buy."+nowday+".daxiuwei",buyed);
            duihuanPlayerXiuwei(p,10000);
            zhuansheng z =main.PlayerZhaunSheng.get(p);
            int level = z.getLevel();
            int xiuwei = z.getXiuwei();
            data.set("level",level);
            data.set("xiuwei",xiuwei);
            try {
                data.save(file);
                p.sendMessage("§a购买大修为丹成功");
                return true;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return true;
    }
    public static boolean usexiaoxiuwei(Player p){
          int total =  checkitemhas(p,小修为丹());
          if (total>=1){
             take(p,小修为丹(),total);
             int givexiuwei = total*100;
             updatePlayerXiuwei(p,givexiuwei);
          return true;
          }else{
              p.sendMessage("§c背包无小修为丹");
              return false;
          }
    }

    public static boolean exchangexiuwei(Player p ){
        File file = new File(main.plugin.getDataFolder() + "/PlayerData/" + p.getName() + ".yml");
        YamlConfiguration data = YamlConfiguration.loadConfiguration(file);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date day = new Date();
        String nowday = sdf.format(day);

        int buyed = data.getInt("buy."+nowday+".money");
        if (buyed>=3){
            p.sendMessage("§a§l今日金币兑换修为次数已达到上限");
            return  false;
        }
        if (main.plugin.economy.getBalance(p)>=200000) {
            main.plugin.economy.withdrawPlayer(p,200000);
            buyed = buyed + 1;
            data.set("buy."+nowday+".money",buyed);
            duihuanPlayerXiuwei(p,2000);
            zhuansheng z =main.PlayerZhaunSheng.get(p);
            int level = z.getLevel();
            int xiuwei = z.getXiuwei();
            data.set("level",level);
            data.set("xiuwei",xiuwei);
            try {
                data.save(file);
                p.sendMessage("§a兑换修为成功");
                return true;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return true;
    }
    public static  int checkitemhas (Player p ,String Name ) {
        Inventory inv = p.getInventory();
        int tocalamo = 0;
        int Max = inv.getSize() - 1;
        for (int i = 0; i < Max; i++)
        {
            ItemStack item = inv.getItem(i);
            if (item != null) {
                if (item.hasItemMeta()) {
                    if (item.getItemMeta().hasDisplayName()) {
                        if (item.getItemMeta().getDisplayName().equalsIgnoreCase(Name)) {
                            tocalamo =tocalamo+item.getAmount();

                        }



                    }
                }
            }
        }



        return tocalamo;
    }
    public static  void take(Player p, String Name, int amo) {
        Inventory playerInventory = p.getInventory();
        int Max = playerInventory.getSize() - 1;
        int takeamo = amo;
        for (int i = 0; i < Max; i++) {

            ItemStack item = playerInventory.getItem(i);
            if (item != null &&
                    item.hasItemMeta() &&
                    item.getItemMeta().hasDisplayName() &&

                    item.getItemMeta().getDisplayName().equalsIgnoreCase(Name) &&
                    item.getAmount() > 0)
                if (takeamo != 0) {
                    if (item.getAmount() == 1) {
                        p.getInventory().setItem(i, null);
                        takeamo--;
                    }
                    else {

                        if (item.getAmount() - takeamo >= 0) {
                            item.setAmount(item.getAmount() - takeamo);
                            p.getInventory().setItem(i, item);

                            break;
                        }
                        p.getInventory().setItem(i, null);
                        takeamo -= item.getAmount();
                    }
                } else {
                    break;
                }
        }
    }

}
